#include "viewer.h"
#include "paintmaskmenu.h"

PaintMaskMenu::PaintMaskMenu()
{
  m_Viewer = 0;

  m_tag = 1;
  m_thickness = 5;
  m_paintArea = false;
  m_depthThreshold = 0.001f;
  m_dradius = 50;

  m_paintAreaActive = false;
  m_tagActive = false;
  m_thicknessActive = false;
  m_dthreshActive = false;

  m_number = 0;
  m_numberText = "0";
}

PaintMaskMenu::~PaintMaskMenu()
{
}

void
PaintMaskMenu::reset()
{
  m_paintAreaActive = false;
  m_tagActive = false;
  m_thicknessActive = false;
  m_dthreshActive = false;
  m_dradiusActive = false;

  m_number = 0;
  m_numberText = "0";
}

void
PaintMaskMenu::drawMenu()
{
  if (!m_Viewer)
    return;

  QFont tfont = QFont("Helvetica",
		      12,
		      QFont::DemiBold);
  tfont.setStyleStrategy(QFont::PreferAntialias);  

  QFontMetrics fm(tfont);
  int ht = fm.height();
  int cht = 20;
  
  cht += 2*ht;
  int lft = 5;
  int ht0 = cht-0.5*ht;
  int ht1 = ht0 + 0.7*ht;  
  int rgt = lft + fm.width("1234567890123456");
  if (m_paintArea)
    ht1 = ht0 + 2.7*ht;

  m_Viewer->startScreenCoordinatesSystem();

  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // blend on top
  glColor4f(0.0f, 0.0f, 0.0f, 0.7f);
  glBegin(GL_QUADS);
  glVertex2f(lft, ht0-7);
  glVertex2f(rgt, ht0-7);
  glVertex2f(rgt, ht1+7);
  glVertex2f(lft, ht1+7);
  glEnd();
  glLineWidth(1);
  glColor4f(0.7f, 0.87f, 0.9f, 0.9f);
  glBegin(GL_LINE_STRIP);
  glVertex2f(lft, ht0-7);
  glVertex2f(rgt, ht0-7);
  glVertex2f(rgt, ht1+7);
  glVertex2f(lft, ht1+7);
  glVertex2f(lft, ht0-7);
  glEnd();


  QString str;

  //------------
  if (m_paintArea)
    str = "Paint Area (YES)";
  else
    str = "Paint Area (NO)";
  m_paintAreaRect = QRect(10, cht-ht,
			  fm.width(str), ht);
  
  if (m_paintAreaActive)
    glColor3f(0, 1, 0.3f);
  else
    glColor3f(1,1,1);

  m_Viewer->drawText(10, cht, str, tfont);
  //------------
  
//  cht += ht;
//
//  //------------
//  str = QString("Tag (%1)").arg(m_tag);
//  m_tagRect = QRect(10, cht-ht,
//		    fm.width(str), ht);
//
//  if (m_tagActive)
//    {
//      glColor3f(0, 1, 0.3);
//      str = QString("Tag (%1)").arg(m_numberText);
//    }
//  else
//    glColor3f(1,1,1);
//
//  m_Viewer->drawText(10, cht, str, tfont);
//  //------------
//  
//  cht += ht;
//
//  //------------
//  str = QString("Thickness (%1)").arg(m_thickness);
//  m_thicknessRect = QRect(10, cht-ht,
//		    fm.width(str), ht);
//
//  if (m_thicknessActive)
//    {
//      glColor3f(0, 1, 0.3);
//      str = QString("Thickness (%1)").arg(m_numberText);
//    }
//  else
//    glColor3f(1,1,1);
//
//  m_Viewer->drawText(10, cht, str, tfont);
//  //------------

  if (m_paintArea)
    {
      cht += ht;

      //------------
      str = QString("Delta (%1)").arg(m_depthThreshold);
      m_dthreshRect = QRect(10, cht-ht,
			    fm.width(str), ht);
      
      if (m_dthreshActive)
	{
	  glColor3f(0, 1, 0.3f);
	  str = QString("Delta (%1)").arg(m_numberText);
	}
      else
	glColor3f(1,1,1);
      
      m_Viewer->drawText(10, cht, str, tfont);
      //------------
      
      cht += ht;
      
      //------------
      str = QString("Spread (%1)").arg(m_dradius);
      m_dradiusRect = QRect(10, cht-ht,
			    fm.width(str), ht);
      
      if (m_dradiusActive)
	{
	  glColor3f(0, 1, 0.3f);
	  str = QString("Spread (%1)").arg(m_numberText);
	}
      else
	glColor3f(1,1,1);
      
      m_Viewer->drawText(10, cht, str, tfont);
      //------------
    }

  m_Viewer->stopScreenCoordinatesSystem();
}


bool
PaintMaskMenu::checkPaintArea(int x, int y)
{
  if (m_paintAreaRect.contains(x, y))
    {
      m_paintAreaActive = true;
      return true;
    }
  
  m_paintAreaActive = false;
  return false;  
}

bool
PaintMaskMenu::checkTag(int x, int y)
{
  if (m_tagRect.contains(x, y))
    {
      m_tagActive = true;
      m_number = m_tag;
      m_numberText = QString("%1").arg(m_tag);
      return true;
    }

  m_tagActive = false;
  return false;  
}

bool
PaintMaskMenu::checkThickness(int x, int y)
{
  if (m_thicknessRect.contains(x, y))
    {
      m_thicknessActive = true;
      m_number = m_thickness;
      m_numberText = QString("%1").arg(m_thickness);
      return true;
    }

  m_thicknessActive = false;
  return false;  
}

bool
PaintMaskMenu::checkDThreshold(int x, int y)
{
  if (! m_paintArea) return false;

  if (m_dthreshRect.contains(x, y))
    {
      m_dthreshActive = true;
      m_number = m_depthThreshold;
      m_numberText = QString("%1").arg(m_depthThreshold);
      return true;
    }

  m_dthreshActive = false;
  return false;  
}

bool
PaintMaskMenu::checkDRadius(int x, int y)
{
  if (! m_paintArea) return false;

  if (m_dradiusRect.contains(x, y))
    {
      m_dradiusActive = true;
      m_number = m_dradius;
      m_numberText = QString("%1").arg(m_dradius);
      return true;
    }

  m_dradiusActive = false;
  return false;  
}

bool
PaintMaskMenu::menuActive()
{
  return (m_paintAreaActive ||
//	  m_tagActive ||
//	  m_thicknessActive ||
	  m_dthreshActive ||
	  m_dradiusActive);
}


bool
PaintMaskMenu::processKey(QKeyEvent *event)
{
  int key = event->key();
  
  if (!menuActive())
    return false;


  if (key == Qt::Key_Return ||
      key == Qt::Key_Enter)
    {
      m_number = 0;
      m_numberText = "0";
      return true;
    }

  if (key == Qt::Key_Backspace)
    {
      m_numberText.chop(1);
      m_number = m_numberText.toFloat();

      if (m_tagActive)
	m_tag = m_number;
      else if (m_thicknessActive)
	m_thickness = m_number;
      else if (m_dthreshActive)
	m_depthThreshold = m_number;
      else if (m_dradiusActive)
	m_dradius = m_number;

      return true;
    }

  bool ignore = false;

  if (key < Qt::Key_0 ||
      key > Qt::Key_9)
    {
      if (m_dthreshActive)
	{
	  if (key != Qt::Key_Period)
	    ignore = true;	
	}
      else
	ignore = true;
    }

  if (ignore)
    return false;
  
  m_numberText += key;
  m_number = m_numberText.toFloat();


  if (m_tagActive)
    m_tag = m_number;
  else if (m_thicknessActive)
    m_thickness = m_number;
  else if (m_dthreshActive)
    m_depthThreshold = m_number;
  else if (m_dradiusActive)
    m_dradius = m_number;


  return true;
}

